/**
 * Created by Baggio on 2020-5-22.
 */
/**
 * Created by Baggio on 2019-10-1.
 */
$(function () {
    var counting = false;
    $("#getCode").click(function () {
        var timer, count = 60,
            that = $(this);
        var phone =$("#phone").val();

        if(!checkPhone(phone)){alert("手机号码有误，请重填");return;}
        console.log(phone);
        $(this).attr("disabled", true);
        var postData={};
        $.ajax({
            type: "get",
            url: "/weChat/regedit/code/"+phone,
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                console.log(res);
                if (res.code != 200) {
                    alert(res.msg);
                    $btn.text('获取验证码');
                    $('.content .info_bar .code_btn').attr("disabled", false);
                    return;
                }
                console.log(res.data);

                /* 倒计时 */
                /*延迟一秒问题*/
                setTimeout(function () {
                    that.html(count + '(s)');
                }, 0)
                /*一分钟后再次发送*/
                timer = setInterval(function () {
                    if (count > 1) {
                        count--;
                        // alert($(this).html())
                        that.html(count + '(s)');
                    } else {
                        counting = false;
                        count = 60;
                        that.html("获取验证码");

                        clearInterval(timer);
                        $('.content .info_bar .code_btn').attr("disabled", false);
                    }

                }, 1000);

            }

        });

    });

    $("#regedit").click(function () {

        var phone =$("#phone").val();
        var password =$("#password").val();
        var password2 =$("#password2").val();
        var code =$("#code").val();
        var realName =$("#realName").val();
        if(realName==undefined){
            aler("请输入姓名！");return;
        }
        if(phone=="" ||phone==undefined || phone==null){alert("手机号码不能为空，请重填");return;}
        if(!checkPhone(phone)){alert("手机号码有误，请重填");return;}
        if(password=="" ||password==undefined || password==null){alert("密码不能为空，请重填");return;}
        if(password2=="" ||password2==undefined || password2==null){alert("密码不能为空，请重填");return;}
        if(password2!=password ){alert("密码不一致，请重填");return;}
        if(password.length!==6){
            alert("密码只能是6位数字，请重填");return;
        }
        if(!isNumber(password)){
            alert("密码只能是6位数字，请重填");return;
        }
        if(code=="" ||code==undefined || code==null){alert("验证码不能为空，请重填");return;}

        console.log(phone);

        var postData={"phone":phone,"password":password,"code":code,"realName":realName};
        console.log(postData);
        $.ajax({
            type: "post",
            url: "/weChat/regedit/regedit",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                console.log(res);
                if (res.code != 200) {
                    alert(res.msg);
                    return;
                }
                console.log(res.data);
                var toUrl=res.data;

                alert("注册成功！");
                window.location.href=toUrl;
            }  ,    error:function () {
                console.log("error===========>>>");
                alert("系统调用错误！");
            }

        });

    });
})